package com.td.module.farmer.service.plantinginformation;

import com.td.framework.common.pojo.PageResult;
import com.td.module.farmer.controller.admin.plantinginformation.vo.*;
import com.td.module.farmer.convert.plantinginformation.PlantingInformationConvert;
import com.td.module.farmer.dal.dataobject.plantinginformation.PlantingInformationDO;
import com.td.module.farmer.dal.mysql.plantinginformation.PlantingInformationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.td.module.farmer.enums.ErrorCodeConstants.PLANTING_INFORMATION_NOT_EXISTS;

/**
 * 种植信息 Service 实现类
 *
 * @author 王翔翔
 */
@Service
@Validated
public class PlantingInformationServiceImpl implements PlantingInformationService {

    @Resource
    private PlantingInformationMapper plantingInformationMapper;

    @Override
    public Long createPlantingInformation(PlantingInformationCreateReqVO createReqVO) {
        // 插入
        PlantingInformationDO plantingInformation = PlantingInformationConvert.INSTANCE.convert(createReqVO);
        plantingInformationMapper.insert(plantingInformation);
        // 返回
        return plantingInformation.getId();
    }

    @Override
    public void updatePlantingInformation(PlantingInformationUpdateReqVO updateReqVO) {
        // 校验存在
        this.validatePlantingInformationExists(updateReqVO.getId());
        // 更新
        PlantingInformationDO updateObj = PlantingInformationConvert.INSTANCE.convert(updateReqVO);
        plantingInformationMapper.updateById(updateObj);
    }

    @Override
    public void deletePlantingInformation(Long id) {
        // 校验存在
        this.validatePlantingInformationExists(id);
        // 删除
        plantingInformationMapper.deleteById(id);
    }

    private void validatePlantingInformationExists(Long id) {
        if (plantingInformationMapper.selectById(id) == null) {
            throw exception(PLANTING_INFORMATION_NOT_EXISTS);
        }
    }

    @Override
    public PlantingInformationDO getPlantingInformation(Long id) {
        return plantingInformationMapper.selectById(id);
    }

    @Override
    public List<PlantingInformationDO> getPlantingInformationList(Collection<Long> ids) {
        return plantingInformationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PlantingInformationDO> getPlantingInformationPage(PlantingInformationPageReqVO pageReqVO) {
        return plantingInformationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PlantingInformationDO> getPlantingInformationList(PlantingInformationExportReqVO exportReqVO) {
        return plantingInformationMapper.selectList(exportReqVO);
    }

    @Override
    public List<PlantingInformationDO> getAllPlanting(Long farmerId) {
        return plantingInformationMapper.selectListPlating(farmerId);
    }

    @Override
    public PlantingInformationSumVO sumLandAndArea() {
        return plantingInformationMapper.sumLandAndArea();
    }

}
